package com.itheima.jobs;

import com.itheima.constrant.RedisConstant;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisPool;

import java.util.Date;
import java.util.Set;

/**
 * @Company www.itheima.com
 * @Author itheima
 */
@Component
public class JobDemo {
    @Autowired
    private JedisPool jedisPool;
    //执行方法的规则
    @Scheduled(cron = "* * * * * ?")
    public void test(){
        System.out.println("time :" + new Date());
        //根据Redies中两个set集合进行差值计算，获得垃圾图片集合
        Set<String> sdiff = jedisPool.getResource().sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        if (sdiff != null) {
            for (String picName : sdiff) {
                //删除七牛云的垃圾图片
                QiniuUtils.deleteFileFromQiniu(picName);
                //删除redis的垃圾图片名称
                jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES, picName);
                System.out.println("清理垃圾图片"+picName);
            }
        }
    }


    //运行Spring容器
    public static void main(String[] args) {
        new ClassPathXmlApplicationContext("spring-task.xml");
    }

}